<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="商城名称" prop="mallName">
        <el-input
          v-model="queryParams.mallName"
          placeholder="请输入商城名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否上线" prop="isUpOnline">
        <el-select v-model="queryParams.isUpOnline" placeholder="请选择">
          <el-option label="是" :value="1"> </el-option>
          <el-option label="否" :value="0"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上线时间" prop="startTime">
        <el-date-picker
          clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择上线时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="下线时间" prop="endTime">
        <el-date-picker
          clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择下线时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:mall:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:mall:edit']"
          >修改</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- <el-table
      v-loading="loading"
      :data="mallList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="商城名称" align="center" prop="mallName" />
      <el-table-column label="商城描述" align="center" prop="mallDetail" />
      <el-table-column
        label="商品入口访问路径"
        align="center"
        prop="mallEntryPath"
      />
      <el-table-column label="商城位" align="center" prop="mallLocation" />
      <el-table-column
        label="是否上线(1是 0否)"
        align="center"
        prop="isUpOnline"
      />
      <el-table-column
        label="上线时间"
        align="center"
        prop="startTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="下线时间"
        align="center"
        prop="endTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="支持的支付渠道"
        align="center"
        prop="payChannel"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:mall:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:mall:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table> -->
    <!-- 商品列表 -->
    <div class="box">
      <div class="box-item" v-for="(item, index) in mallList" :key="index">
        <el-card class="box-card">
          <img
            src="https://lanhu-dds-backend.oss-cn-beijing.aliyuncs.com/merge_image/imgs/45bda102943a4eb6a95d82e6165ffbd6_mergeImage.png"
            alt=""
            class="img"
          />
          <!-- {{ mallList }} -->
          <div class="jie-shao">
            <!-- <div class="size">田芮乾</div> -->

            <div class="size-it">
              <span>商城名称：</span>
              <span class="span">{{ item.mallName }}</span>
            </div>
            <div class="size-it">
              <span>商品入口：</span>
              <span class="span">{{
                `${item.mallEntryPath}?mall_id=${item.id}`
              }}</span>
            </div>
            <div class="size-it">
              <span>上线时间：</span>
              <span class="span">{{ parseTime(item.startTime) }}</span>
            </div>
            <div class="size-it">
              <span>下线时间：</span>
              <span class="span">{{ parseTime(item.endTime) }}</span>
            </div>
            <div class="size-it">
              <span>在线状态：</span>
              <span class="span" v-if="item.isUpOnline == 1">在线中</span>
              <span class="span" v-if="item.isUpOnline == 0">已下线</span>
            </div>
            <div class="size-it">
              <span>商城简介：</span>
              <span class="span">{{ item.mallDetail }}</span>
            </div>
            <div class="size-it">
              <span>PC端入口：</span>
              <span class="span">{{ item.mallPcEntry }}</span>
            </div>
            <div class="box2">
              <div class="size-guanli" @click="djGli(item)">管理中心</div>
              <div
                class="size-guanli"
                @click="handleUpdate(item)"
                v-hasPermi="['system:mall:edit']"
              >
                修改
              </div>
            </div>
          </div>
        </el-card>
      </div>
    </div>
    <div style="clear: both"></div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      :pageSizes.sync="pageSizes"
      @pagination="getList"
    />

    <!-- 添加或修改商城管理对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="商城名称" prop="mallName">
              <el-input v-model="form.mallName" placeholder="请输入商城名称" />
            </el-form-item>

            <!-- <el-form-item label="商城入口" prop="mallEntryPath">
              <el-input
                v-model="form.mallEntryPath"
                placeholder="请输入商品入口访问路径-链接"
              />
            </el-form-item> -->
            <el-form-item label="上线时间" prop="startTime">
              <el-date-picker
                clearable
                v-model="form.startTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择上线时间"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="下线时间" prop="endTime">
              <el-date-picker
                clearable
                v-model="form.endTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择下线时间"
              >
              </el-date-picker>
            </el-form-item>

            <el-form-item label="积分比例" prop="integralRatio" v-if="isJifen">
              <el-row>
                <el-col :span="8">
                  <el-input value="1" disabled></el-input>
                </el-col>
                <el-col :span="2" style="text-align: center">：</el-col>
                <el-col :span="14">
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="提示:1元 : 10积分,如果商品价格为10元，则积分数为10*10=100，该商品积分数为100积分"
                    placement="top-start"
                  >
                    <el-input
                      v-model="form.integralRatio"
                      type="number"
                      onkeyup="value=value.replace(/[^\d]/g,'')"
                      placeholder="请输入积分比例"
                    />
                  </el-tooltip>
                </el-col>
              </el-row>
            </el-form-item>
            <el-form-item
              label="所属客户"
              prop="customerId"
              v-hasPermi="['system:mall:notCustomer']"
            >
              <el-select v-model="form.customerId" placeholder="请选择">
                <el-option
                  v-for="item in customerList"
                  :key="item.id"
                  :label="item.customerName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="商城模板">
              <el-select v-model="form.chooseMallId" placeholder="请选择">
                <el-option
                  v-for="item in mallList"
                  :key="item.id"
                  :label="item.mallName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否上线" prop="isUpOnline">
              <el-radio v-model="form.isUpOnline" :label="1">是</el-radio>
              <el-radio v-model="form.isUpOnline" :label="0">否</el-radio>
            </el-form-item>

            <el-form-item label="支付方式" prop="payChannel">
              <el-select
                v-model="payChannel"
                multiple
                placeholder="请选择支付方式"
                @change="changePayChannel"
              >
                <el-option
                  v-for="item in payType"
                  :key="item.id"
                  :label="item.payTypeName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="礼品公司"
              prop="giftId"
              v-hasPermi="['system:mall:notCustomer']"
            >
              <el-select
                v-model.number="form.giftId"
                placeholder="请选择礼品公司"
              >
                <el-option
                  v-for="item in giftCompanyList"
                  :key="item.id"
                  :label="item.giftCompanyName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="账号" v-hasPermi="['system:mall:customer']">
              <el-input
                v-model="form.shopAccount"
                placeholder="请输入账号"
                style="width: 217px"
              ></el-input>
            </el-form-item>
            <el-form-item label="密码" v-hasPermi="['system:mall:customer']">
              <el-input
                v-model="form.shopPwd"
                style="width: 217px"
                type="password"
                show-password
                autocomplete="new-password"
                placeholder="请输入密码"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- form.payChannel -->
        <el-form-item label="商城描述" prop="mallDetail">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="form.mallDetail"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMall,
  delMall,
  addMall,
  getMallPayType,
  updateMall,
  getGiftCompanyList,
} from "@/api/system/mall";
import { mallIndex } from "@/api/system/product";
import { listUserAll } from "@/api/system/user";
import { listAllType } from "@/api/mallSystem/mallType";
import { listAllCustomer } from "@/api/customer/employee";
import { getCustomerShopList } from "@/api/newGoods/goodsList";
export default {
  name: "Mall",
  data() {
    const startTimeValidate = (rule, value, callback) => {
      if (this.form.isUpOnline == 1) {
        if (value == null) {
          callback(new Error("请选择上线时间"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    const endTimeValidate = (rule, value, callback) => {
      if (this.form.isUpOnline == 1) {
        if (value == null) {
          callback(new Error("请选择下线时间"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    // const loginTypeValidate = (rule, value, callback) => {
    //   if (this.form.isCanLogin == 1) {
    //     if (value == null) {
    //       callback(new Error("请输入登录方式"));
    //     } else {
    //       callback();
    //     }
    //   } else {
    //     callback();
    //   }
    // };
    return {
      pageSizes: [9, 18, 27, 36],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      radio: 1,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商城管理表格数据
      mallList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 9,
        mallName: null,
        isUpOnline: null,
        startTime: null,
        endTime: null,
      },
      // 表单参数
      form: {
        payChannel: [],
      },
      customerList: [],
      payChannel: [],
      //支付方式
      payType: [],
      //是否展示积分
      isJifen: false,
      //用户列表
      userList: [],
      //商城类型列表
      typeList: [],
      // 礼品公司列表
      giftCompanyList: [],
      // 表单校验
      rules: {
        mallName: [
          { required: true, message: "请输入商城名称", trriger: "blur" },
        ],
        // mallType: [
        //   {
        //     required: true,
        //     message: "请选择商城类型",
        //     trriger: "change",
        //     type: "number",
        //   },
        // ],
        // mallLocation: [
        //   { required: true, message: "请输入商城位置", trriger: "blur" },
        // ],
        // mallEntryPath: [
        //   { required: true, message: "请输入商城入口", trriger: "blur" },
        // ],
        mallBudget: [
          { required: true, message: "请输入商城预算", trriger: "blur" },
        ],
        startTime: [{ validator: startTimeValidate }],
        endTime: [{ validator: endTimeValidate }],
        payChannel: [
          {
            required: true,
            message: "请选择支付方式",
            trriger: "change",
          },
        ],
        integralRatio: [
          { required: true, message: "请输入积分比例", trriger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (value <= 0 || value > 100000) {
                return callback(new Error("请输入1-100000之间的整数"));
              } else {
                return callback();
              }
            },
          },
        ],
        // loginType: [{ validator: loginTypeValidate, trriger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
    this.getAllUserList();
    this.getAllTypeList();
  },
  watch: {
    payChannel(newVal, oldVal) {
      if (newVal.includes(1) || newVal.includes(2) || newVal.includes(3)) {
        this.isJifen = true;
      } else {
        this.isJifen = false;
      }
    },
  },
  methods: {
    getMallList() {
      getCustomerShopList().then((res) => {
        this.mallList = res.data;
      });
    },
    // 查询所有客户列表
    getAllCustomerList() {
      listAllCustomer().then((res) => {
        this.customerList = res.data;
      });
    },
    changePayChannel(val) {
      this.form.payChannel = val.join(",");
    },
    // 获取礼品公司列表
    getGiftCompanyList() {
      getGiftCompanyList().then((res) => {
        this.giftCompanyList = res.data;
      });
    },
    /** 查询商城管理列表 */
    getList() {
      this.loading = true;
      listMall(this.queryParams).then((response) => {
        console.log(response.rows);
        this.mallList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取支付方式列表
    getPayType() {
      getMallPayType().then((res) => {
        this.payType = res.data;
      });
    },
    //获取用户列表
    getAllUserList() {
      listUserAll().then((res) => {
        this.userList = res.data;
      });
    },
    //获取商城类型所有列表
    getAllTypeList() {
      listAllType().then((res) => {
        this.typeList = res.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.getList();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        mallName: null,
        mallDetail: null,
        mallEntryPath: null,
        mallLocation: null,
        isUpOnline: 1,
        startTime: null,
        endTime: null,
        payChannel: null,
        isOpenAfterSale: 1,
        isShowSupplierLogo: 1,
        payChannel: [],
      };
      this.resetForm("form");
    },
    // 管理中心
    djGli(data) {
      const { id, backUserId } = data;
      mallIndex({ mallId: id }).then(() => {
        const { href } = this.$router.resolve({
          path: "/product/product",
          query: {
            id,
            backUserId,
          },
        });
        window.open(href, "_blank");
      });
    },
    change(e) {
      console.log(e);
      let cc = e.join(",");
      this.payChannel = cc;
      console.log(this.payChannel);
      // console.log(cc);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.payChannel = [];
      this.getPayType();
      this.open = true;
      this.title = "添加商城管理";
      this.getGiftCompanyList();
      this.getAllCustomerList();
      this.getMallList();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // payChannel
      this.getPayType();
      this.reset();
      this.form = row;
      this.payChannel = row.payChannel.toString().split(",").map(Number);

      this.open = true;
      this.title = "修改商城管理";
      this.getGiftCompanyList();
      this.getAllCustomerList();
      this.getMallList();
      // const id = row.id || this.ids;
      // getMall(id).then((response) => {
      //   // payChannel
      //   console.log(response.payChannel);

      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        this.form.payChannel = this.payChannel.join(",");
        if (valid) {
          if (this.form.id != null) {
            updateMall(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMall(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除商城管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delMall(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/mall/export",
        {
          ...this.queryParams,
        },
        `mall_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.box {
  width: 100%;
  // margin-top: 20px;
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
  .box-item {
    width: 32%;
    // height: 300px;
    margin: 10px 10px 0 0;
    .box-card {
      width: 100%;
      .img {
        width: 100%;
      }
      .jie-shao {
        .size {
          font-size: 18px;
          font-weight: 600;
          margin-top: 10px;
        }
        .box2 {
          display: flex;
          justify-content: space-between;
          flex-wrap: wrap;
          .size-guanli {
            margin-top: 25px;
            width: 47%;
            height: 40px;
            line-height: 40px;
            text-align: center;
            border: 1px solid skyblue;
            color: skyblue;
            cursor: pointer;
          }
        }
        .size-it {
          width: 100%;
          display: flex;
          justify-content: flex-start;
          // justify-content: space-between;
          margin: 10px 0;
          span {
            // width: 22%;
            font-size: 14px;
            display: block;
            color: #999999;
          }
          .span {
            // width: 76%;
            display: block;
            font-size: 14px;
            color: #000;
          }
        }
      }
    }
  }
}
</style>
